package org.csu.molediary.api.common;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

@Getter
//JSON序列化时空的数据不被包含
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable {
    private int code;
    private String msg;
    private T data;

    private CommonResponse(int code){
        this.code = code;
    }

    private CommonResponse(int code,String msg){
        this.code = code;
        this.msg = msg;
    }

    private CommonResponse(int code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private CommonResponse(int code,T data){
        this.code = code;
        this.data = data;
    }

    //isSuccess不被序列化
    @JsonIgnore
    public boolean isSuccess(){//判断响应是不是成功的,true则是成功
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    public static <T> CommonResponse <T> createForSuccess(){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> CommonResponse <T> createForSuccess(T data){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> CommonResponse <T> createForSuccessMessage(String msg){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> CommonResponse <T> createForSuccess(String msg,T data){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> CommonResponse <T> createForError(){
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription());
    }

    public static <T> CommonResponse <T> createForError(String msg){
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }

    public static <T> CommonResponse <T> createForError(int code,String msg){
        return new CommonResponse<T>(code,msg);
    }

    public static <T> CommonResponse <T> createForAdminSuccess(String msg,T data){
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),msg,data);
    }

    public static <T> CommonResponse <T> createForAdminSuccessMessage(String msg){
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }

    public static <T> CommonResponse <T> createForAdminError(String msg){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
}
